package hypermediaresearch.controller.hypermedia;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/*
 * VinhJune 2012
 */
import hypermediaresearch.model.Project;
import hypermediaresearch.service.PartnerService;
import hypermediaresearch.service.ProjectService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

public class All_projectsController extends Controller {
   
    private ProjectService prjService = new ProjectService();
    private PartnerService ptnService = new PartnerService();
    
    @Override
    public Navigation run() throws Exception {
        
        String name = requestScope("name");
        String year = requestScope("year");
        List<Project> projectList = new ArrayList<Project>();
        if((name != "") && (name != null)) {
            projectList = ptnService.getCoordinatedProjectList(ptnService.getPartnerByName(name));
        } else if ((year !="") && (year != null)){
            projectList = prjService.getProjectByYear(year);
        } else {
            projectList = prjService.getProjectList();
            for (Iterator iterator = projectList.iterator(); iterator.hasNext();) {
                Project project = (Project) iterator.next();
                if (project.getContent().length() > 256) {
                    project.setContent(project.getContent().substring(0, 255));
                }
            }
        }
        requestScope("projectList", projectList);
        requestScope("partner", name);
        requestScope("year", year);
        return forward("all_projects.jsp");
    }
}
